//
//  LiveHandle.m
//  BasketballClub
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 孙鸿吉. All rights reserved.
//

#import "LiveHandle.h"
#import <AFNetworking.h>
#import "LiveModel.h"
#import "CoreDataManager.h"
static LiveHandle *handle = nil;
@implementation LiveHandle
//单例的初始化
+(instancetype)sharedLiveHandle
{
    return [[self alloc]init];
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (handle == nil) {
            handle = [super allocWithZone:zone];
        }
        
    });
    return handle;
   
}


#pragma mark - 请求数据的方法
///昨日
-(void)GetYesterdayDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
     //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    //创建字典
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
    //对数据进行POST请求
        [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
     //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
    //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
            //提取数据中的关键字段
  
            NSArray *array = resultDic[@"list"];
            //提取数组中的第1个元素
            NSDictionary *yesterdayDict = array[0];
       
            //创建临时可变数组
            NSMutableArray *tempArray  = [NSMutableArray array];
            
             NSArray *tempArr = yesterdayDict[@"tr"];
            //遍历数组
            for (NSDictionary *dic in tempArr) {
            //KVC赋值
                LiveModel *model = [[LiveModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];

                [tempArray addObject:model];
                
            }
            //把数据储存到数组
                self.liveYesterdayDataArray = [NSArray arrayWithArray:tempArray];
           
            
            
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveYesterdayDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}
#pragma mark - 请求数据的方法
///今日
-(void)GetTodayDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置数据返回格式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    //创建字典
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
    //对数据进行POST请求
    [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
        //提取数组中的第二个元素
        NSDictionary *yesterdayDict = array[1];
        
        //遍历数组
        NSMutableArray *tempArray  = [NSMutableArray array];
        NSArray *tempArr = yesterdayDict[@"tr"];
        
        for (NSDictionary *dic in tempArr) {
            //KVC赋值
            LiveModel *model = [[LiveModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [tempArray addObject:model];
            
        }
        //储存数据
        self.liveTodayDataArray = [NSArray arrayWithArray:tempArray];
       
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveTodayDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}


#pragma mark - 请求数据的方法
///明日
-(void)GetTomorrowDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    //创建字典
    NSDictionary *dictionary = [NSDictionary dictionaryWithObject:@"476cbbdbfb38f73accf694e4abb6b6b9" forKey:@"key"];
    //对数据进行POST请求
    [sessionManager POST:@"http://op.juhe.cn/onebox/basketball/nba?" parameters:dictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
        //提取数组中的第3个元素
        NSDictionary *yesterdayDict = array[2];
        
        //创建临时可变数组
        NSMutableArray *tempArray  = [NSMutableArray array];
        NSArray *tempArr = yesterdayDict[@"tr"];
        //遍历数组
        for (NSDictionary *dic in tempArr) {
            
            LiveModel *model = [[LiveModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [tempArray addObject:model];
            
        }
        //把数据储存到数组
        self.liveTomorrowDataArray = [NSArray arrayWithArray:tempArray];
                 
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.liveTomorrowDataArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
    
}


///请求数据
-(void)getDataFromNetWorkWithBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:self.urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
       
        //创建临时可变数组
        NSMutableArray *tempArray  = [NSMutableArray array];
//
        //遍历数组
        for (NSDictionary *dic in array) {
            
            LiveModel *model = [[LiveModel alloc]init];
            //KVC赋值
            [model setValuesForKeysWithDictionary:dic];
            //把 model添加到可变数组
            [tempArray addObject:model];
           
        }
       // 把数据储存到数组
        self.battleDetailsArray = [NSArray arrayWithArray:tempArray];
       
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败?????????");
    }];
    
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.battleDetailsArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });


}


-(void)getDataFromNetWorkWithUrlStr:(NSString*)urlString withBlock:(LIVEBLOCK)finishBlock
{
    //创建管理类
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    //设置返回方式
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    [sessionManager GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //转化数据格式
        NSDictionary *dict = (NSDictionary*)responseObject;
        //提取数据字典中的关键字段
        NSDictionary *resultDic = dict[@"result"];
        //提取数据中的关键字段
        NSArray *array = resultDic[@"list"];
        
        //创建临时可变数组
        NSMutableArray *tempArray  = [NSMutableArray array];
        //
        //遍历数组
        for (NSDictionary *dic in array) {
            
            LiveModel *model = [[LiveModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dic];
        
            [tempArray addObject:model];
            
        }
        // 把数据储存到数组
        self.battleDetailsArray = [NSArray arrayWithArray:tempArray];
               
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
    }];
    //回到主线程
    dispatch_async(dispatch_get_main_queue(), ^{
        
        if (self.battleDetailsArray.count> 0) {
            
            finishBlock(YES);
            
        }else{
            
            finishBlock(NO);
        }
        
    });
}








@end
